package com.lzc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.mapper.FriendMapper;
import com.lzc.mapper.UserMapper;
import com.lzc.pojo.dto.SearchUserDto;
import com.lzc.pojo.dto.UserDto;
import com.lzc.pojo.entity.Friend;
import com.lzc.pojo.entity.User;
import com.lzc.pojo.vo.Result;
import com.lzc.service.UserService;
import com.lzc.util.ThreadLocalUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FriendMapper friendMapper;

    /**
     * 获取登录的用户信息
     * @return
     */
    @Override
    public Result getUserInfo() {
        Long id = ThreadLocalUtil.getUid();
        // 根据id获取用户信息
        User user = userMapper.selectById(id);
        if (user == null){
            return Result.FILE("");
        }
        UserDto userDto = new UserDto(user);

        return Result.OK(userDto);
    }

    /**
     * 查找用户
     * @param username 账号
     * @return
     */
    @Override
    public Result findUserByUsername(String username) {
        if (StrUtil.isBlank(username)){
            return Result.OK(null);
        }
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(
                User::getUsername, username
        ));
        if (user == null){
            return Result.FILE("用户不存在");
        }
        SearchUserDto searchUserDto = new SearchUserDto(user);
        Long uid = ThreadLocalUtil.getUid();
        Friend friend = friendMapper.isFriend(uid, user.getId());
        if (friend != null){
            searchUserDto.setMyFriend(true);
        }
        return Result.OK(searchUserDto);
    }
}
